package com.ebring.shareEducation.controller;
import com.ebring.shareEducation.utils.RspMsg;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;


@RestController
public class ContractAction {
    /**
     * 图片上传
     *
     * @param file
     * @param request
     * @param response
     * @return
     * @throws IllegalStateException
     * @throws IOException
     */
    @ResponseBody
    @RequestMapping(value = "/photoUpload", method = RequestMethod.POST)
    public RspMsg<Object> photoUpload(MultipartFile file, HttpServletRequest request, HttpServletResponse response) throws IllegalStateException, IOException {
        RspMsg<Object> rspMsg = new RspMsg<>();
        if (file != null) {// 判断上传的文件是否为空
            String path = null;// 文件路径
            String type = null;// 文件类型
            String fileName = file.getOriginalFilename();// 文件原名称
            System.out.println("上传的文件原名称:" + fileName);
            // 判断文件类型
            type = fileName.indexOf(".") != -1 ? fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length()) : null;
            if (type != null) {// 判断文件类型是否为空
                if ("GIF".equals(type.toUpperCase()) || "PNG".equals(type.toUpperCase()) || "JPG".equals(type.toUpperCase())) {
                    // 项目在容器中实际发布运行的根路径
                     String realPath=request.getSession().getServletContext().getRealPath("/");
//                    String realPath = "/picUpload";
                    // 自定义的文件名称
                    String trueFileName = String.valueOf(System.currentTimeMillis()) + fileName;
                    // 设置存放图片文件的路径
//                    path = realPath +/*System.getProperty("file.separator")+*/trueFileName;
                    path = "D:\\EbringCode\\共享教育\\apache-tomcat-9.0.13\\webapps\\pic\\"+trueFileName;
                    System.out.println("存放图片文件的路径:" + path);
                    // 转存文件到指定的路径
                    String jar_v4 = new String(path.getBytes(), "UTF-8");
                    file.transferTo(new File(jar_v4));
                    String httpImgPath = "http://192.168.108.68:8020/pic/" + trueFileName;
                    rspMsg.setOK();
                    rspMsg.setResult(httpImgPath);
                } else {
                    rspMsg.setError();
                    rspMsg.setMsg("不是我们想要的文件类型,请按要求重新上传");
                }
            } else {
                rspMsg.setError();
                rspMsg.setMsg("文件类型为空");
            }
        } else {
            rspMsg.setError();
            rspMsg.setMsg("没有找到相对应的文件");
        }
        return rspMsg;
    }
}
